<?php

namespace addons\booking\controller;

use addons\booking\model\Store as StoreModel;
use addons\booking\model\Worker as WorkerModel;
use addons\booking\model\StoreShield;
use think\Db;
use think\Session;


class Store extends Base
{
    protected $noNeedLogin = ['storeList', 'getSettleStore','detail', 'defaultStore', 'cutStore'];
    protected $modelValidate = true; //是否开启Validate验证，默认是false关闭状态
    protected $modelSceneValidate = true; //是否开启模型场景验证，默认是false关闭状态

    //获取首页数据
    public function index()
    {
        #轮播图
        $banner_where = [];
        $banner_where['page_type'] = ['like','%1'];
        $banner_where['type'] = ['=','1'];
        $banner_where['status'] = ['=',1];
        $banner = Db::name('content_banner')->where($banner_where)->field('id,title,image,url,url_type')->order('weigh','desc')->select();

        #公告
        $notice_where = [];
        $notice_where['status'] = ['=',1];
        $notice_where['page_type'] = ['like','%1'];
        $notice_where['start_time'] = ['<',time()];
        $notice_where['end_time'] = ['>',time()];
        $notice = Db::name('content_notice')->where($notice_where)->field('id,title')->order('id','desc')->select();
        #用户已绑定的酒店列表
        $bind_store_list = [];
        $store_ids = Db::name('store_user')->where('status',1)->column('store_id');

        $bind_store_list = StoreModel::where('id','in',$store_ids)->field('id,name,logo,address')->select();
        foreach ($bind_store_list as $k=>$v)
        {
            $store_user = Db::name('store_user')->where('status',1)->where('work_status',1)->field('id,uid,user_name')->find();
            $bind_store_list[$k]['store_user'] = $store_user ? $store_user :[];
        }
        #公告弹窗
        $pop_notice = [];
        $pop_notice = Db::name('content_notice')->field('id,title,content')->where($notice_where)->order('id','desc')->find();


        $data['banner'] = $banner ? $banner : [];
        $data['notice'] = $notice ? $notice : [];
        $data['bind_store_list'] = $bind_store_list?$bind_store_list:[];
        $data['pop_notice'] = $pop_notice?$pop_notice:[];


        $this->success('', $data);
    }

    /**
     * 门店列表
     */
    public function storeList()
    {
        $this->success('', StoreModel::tableList($this->request->param()));
    }

    //获取已入驻的酒店列表
    public function getSettleStore()
    {
        $this->success('', StoreModel::getSettleStore($this->request->param()));
    }

    /**
     * 切换
     */
    public function cutStore()
    {
        $store_id = $this->request->post('store_id');
        if (!$store_id) {
            $this->error('参数错误');
        }
        $data = StoreModel::getDetail($store_id);
        if (!$data) {
            $this->error('未找到相关门店信息');
        }
        Session::set('store_id', $store_id);
        $this->success('切换成功！');
    }

    /**
     * 默认门店或手动切换的门店
     */
    public function defaultStore()
    {
        $id = Session::get('store_id');
        $latitude = $this->request->param('latitude');
        $longitude = $this->request->param('longitude');
        $PI = M_PI;
        $data = null;
        $fields = "id,name,images,phone,latitude,longitude,address,(2 * 6378.137 * ASIN(SQRT(POW(SIN($PI*({$latitude}-latitude)/360),2)+COS($PI*{$latitude}/180)* COS(latitude * $PI/180)*POW(SIN($PI*({$longitude}-longitude)/360),2)))) as distance";
        if ($id) {
            $data = StoreModel::field('id,name,images,phone,latitude,longitude,address')->where('status', 'normal')->where('id', $id)->find();
        } elseif ($latitude && $longitude) {
            $data = StoreModel::field($fields)->order('distance asc')->where('status', 'normal')->find();
        }
        if (!$data) {
            $data = StoreModel::field('id,name,images,phone,latitude,longitude,address')->where('status', 'normal')->where('is_default', 1)->find();
        }
        if (!$data) {
            $this->error('未找到相关门店信息');
        }
        $data->is_more = (StoreModel::count()) > 1;
        $this->success('', $data);
    }

    /**
     * 详情
     */
    public function detail()
    {
        $id = $this->request->param('id/d');
        if (empty($id)) {
            $this->error('参数缺失');
        }
        $data = StoreModel::getDetail($id);
        if (!$data) {
            $this->error('未找到相关门店信息');
        }
        $this->success('', $data);
    }

    //合作申请
    public function applyStore()
    {
        $request =  $this->request->post();
        $request['user_id'] = $this->auth->id;
        $res = StoreModel::addStore($request);
        if (!$res) {
            $this->error('申请失败');
        }
        $this->success('申请成功');
    }

    //附近的保洁
    public function vicinity_woker()
    {
       $store_id = $this->request->param('store_id/d',0);
       $page = $this->request->param('page/d',1);
       $limit = $this->request->param('limit/d',10);
       $order = $this->request->param('order','asc');
       $keyword = $this->request->param('keywork','');

       $list = WorkerModel::vicinity_wroker($store_id,$page,$limit,$order,$keyword);
       $this->success('',$list);
    }

    //保洁列表
    public function woker_list()
    {
        $store_id = $this->request->param('store_id/d',0);
        $page = $this->request->param('page/d',1);
        $limit = $this->request->param('limit/d',10);
        $order = $this->request->param('order','asc');
        $keyword = $this->request->param('keywork','');
        $type = $this->request->param('type',1);

        $list = WorkerModel::wroker_list($store_id,$page,$limit,$order,$keyword,$type);
        $this->success('',$list);
    }

    //设置值班
    public function set_work_status()
    {
        $store_id = $this->request->param('store_id/d',0);
        $uid = $this->auth->id;
        if(!$store_id)  $this->error('参数错误');
        $store_user = \addons\booking\model\StoreUser::where(['store_id'=>$store_id,'uid'=>$uid])->find();
        if(!$store_user) $this->error('您不是酒店的发单员');

        $res = \addons\booking\model\StoreUser::set_work_status($store_id,$uid);
        if (!$res) {
            $this->error('申请失败');
        } else {
            $this->success('操作成功');
        }

    }

    //关注或屏蔽保洁
    public function store_follow()
    {
        $store_id = $this->request->param('store_id/d',0);
        $worker_id = $this->request->param('worker_id/d',0);
        $type = $this->request->param('type/d',0);
        if(!$store_id || $worker_id) $this->error('参数错误');
        if(!in_array($type,[1,2]))  $this->error('操作错误');

        $where['store_id'] = $store_id;
        $where['worker_id'] = $worker_id;
        $where['type'] = $type;
        $info = StoreShield::where($where)->find();
        if($info){
            if($type == 1 ){
                $this->error('已关注该保洁');
            }else if($type == 2){
                $this->error('已屏蔽该保洁');
            }
            $this->error('数据已存在');
        }

        $map['store_id'] = $store_id;
        $map['worker_id'] = $worker_id;
        $map['type'] = $type;
        $map['create_time'] = time();

        $res = StoreShield::create($map);
        if($res){
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    //解除关注或屏蔽
    public function delete_store_follow()
    {
        $id = $this->request->param('id/d',0);
        if(!$id)  $this->error('参数错误');
        $info =  StoreShield::where('id',$id)->find();
        if(!$info)  $this->error('数据不存在');


        $res = StoreShield::where('id',$id)->delete();
        if($res){
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }

    //关注或屏蔽列表
    public function store_follow_list()
    {
        $store_id = $this->request->param('store_id/d',0);
        $type = $this->request->param('type/d',0);
        $page = $this->request->param('page/d',1);
        $limit = $this->request->param('limit/d',10);
        if(!$store_id) $this->error('参数错误');
        if(!in_array($type,[1,2]))  $this->error('操作错误');

        $where['store_id'] = $store_id;
        $where['type'] = $type;
        $list = StoreShield::alias('a')
             ->where($where)
             ->join('worker w','a.worker_id = w.id','left')
             ->field('a.*,w.user_name,w.head_image')
             ->order('id desc')
             ->paginate($limit)->toArray();
        $this->success('操作成功',$list);
    }

 }
